<template>
  <div class="log-box-style">
    <div class="query-form-style">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="操作描述" prop="description">
          <el-input v-model="queryParams.description" placeholder="请输入操作描述" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="服务名称" prop="service">
          <el-input v-model="queryParams.service" placeholder="请输入请求的服务名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="操作用户" prop="username">
          <el-input v-model="queryParams.username" placeholder="请输入操作用户" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="请求类型" prop="method">
          <el-select v-model="queryParams.method" placeholder="请选择" clearable>
            <el-option
              v-for="item in interface_request_type"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 360px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="handleResetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table ref="adminTable" :data="resultList" style="width: 100%;" border>
      <el-table-column label="操作描述" prop="description" align="center" min-width="100"></el-table-column>
      <el-table-column label="IP" prop="ip" align="center" min-width="100"></el-table-column>
      <el-table-column label="响应消息值" prop="message" align="center" min-width="100"></el-table-column>
      <el-table-column label="响应状态码" prop="statusCode" align="center" min-width="100"></el-table-column>
      <el-table-column label="请求类型" prop="method" align="center" min-width="100"></el-table-column>
      <el-table-column label="请求参数" prop="parameter" align="center" min-width="100">
        <template slot-scope="scope">
          <div :title="scope.row.parameter">{{scope.row.parameter}}</div>
        </template>
      </el-table-column>
      <el-table-column label="返回结果" prop="result" align="center" min-width="100">
        <template slot-scope="scope">
          <div :title="scope.row.result">{{scope.row.result}}</div>
        </template>
      </el-table-column>
      <el-table-column label="请求的服务" prop="service" align="center" min-width="100"></el-table-column>
      <el-table-column label="消耗时间" prop="spendTime" align="center" min-width="100"></el-table-column>
      <el-table-column label="URL" prop="url" align="center" min-width="100"></el-table-column>
      <el-table-column label="操作时间" width="160" align="center">
        <template slot-scope="scope">{{scope.row.operateTime | formatDateTime}}</template>
      </el-table-column>>
      <el-table-column label="操作" width="180" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     @click="handleCopy(scope.$index, scope.row)">
            复制参数
          </el-button>
          <el-button size="mini"
                     type="text"
                     @click="handleCopy(scope.$index, scope.row)">
            复制返回值
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :page-sizes="[10,15,20, 50, 100]"
        :total="resultTotal">
      </el-pagination>
    </div>
  </div>
</template>

<!-- 详情  /operateLog/detail/{id}  -->

<script>
import {formatDate} from '@/utils/date';

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  description: null,
  method: null,
  operateStartTime: null,
  operateEndTime: null,
  service: null,
  username: null,
};

export default {
  name: "index",
  data() {
    return {
      queryParams: Object.assign({}, defaultListQuery),
      resultList: [],
      resultTotal: 0,
      // 日期范围
      dateRange: [],
      interface_request_type: [],
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === '') {
        return 'N/A';
      }
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  created() {
    this.handleQuery();
    this.getDict();
  },
  methods: {
    getDict() {
      this.$commonList("/dict/getListByType", "GET", { codeType: "interface_request_type" }).then(res => {
        if (res.code === 200) {
          this.interface_request_type = res.data;
        }
      })
    },
    /** 搜索 + 分页 */
    handleResetSearch() {
      this.dateRange = [];
      this.queryParams = Object.assign({}, defaultListQuery);
      this.getList()
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.resultList = [];
      let dateRange = Array.isArray(this.dateRange) ? this.dateRange : [];
      this.queryParams.operateStartTime = dateRange[0];
      this.queryParams.operateEndTime = dateRange[1];
      this.$commonList("/operateLog/list", "GET", this.queryParams).then(res => {
        if (res.code === 200) {
          this.resultList = res.data.list;
          this.resultTotal = res.data.total;
        }
      })
    },
    handleSizeChange(val) {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    //  查看日志详情
    handleCopy(index, row) {
      //复制到剪切板
      if (navigator.clipboard) {
        navigator.clipboard.writeText(JSON.stringify(row.parameter)).then(() => {
          this.$message.success("复制成功")
        }, () => {
          this.$message.error("复制失败")
        })
      } else {
        this.$message.error("当前浏览器不支持")
      }
    }
  },
}
</script>

<style lang="scss" scoped>
  .log-box-style {
    color: red;
    padding: 20px;
    .query-form-style {

    }
  }
</style>
